package crack;

import java.io.IOException;

import math2.MathUtilities;
import fem2.AbstractStructuralStaticDemo;
import fem2.Constraint;
import fem2.Element;
import fem2.MaterialModel;
import fem2.Mesh;
import fem2.MeshGenerator;
import fem2.MeshPart;
import fem2.MeshUtilities;
import fem2.Model;
import fem2.element.ConstantCrackElement;
import fem2.enu.EchoLevelType;
import fem2.enu.State;
import fem2.material.StVenantKirchhoffMM;
import fem2.pre_and_post.GidMeshGenerator;

/**
 * 
 * sample mesh of Fabien
 * 
 * @author hbui
 * 
 */
public class FabienSampleConstantCrack extends AbstractLshapeConstantCrack {

	public FabienSampleConstantCrack() {
		projectName = "fabien.gid";
		meshFileName = "surfTri3_3.msh";
	}

	@Override
	public Mesh createMesh() {
		String fn = projectDir + '/' + projectName + '/' + meshFileName;
		MeshGenerator mg = new GidMeshGenerator(fn);
		Mesh mesh = mg.getMesh(2);
		return mesh;
	}

	@Override
	public Model createConditions(Model m) {
		Mesh mesh = m.getMesh();
		/*
		 * conditions
		 */
		Constraint c1 = new Constraint(false, false);
		Constraint c2 = new Constraint(true, false);
		Constraint c3 = new Constraint(true, false);
		double du = 0.01;
		c3.setValue(1, du);
		mesh.setConstraint(c2, MeshUtilities.seekNodesOnSurface(mesh, 0, 1, 0));
		MeshUtilities.seekNode(mesh, 160, 0).setConstraint(c1);
		mesh.setConstraint(c3, MeshUtilities.seekNodesOnSurface(mesh, 0, 1, -300));
		return m;
	}

	@Override
	public MaterialModel createMaterial(Model m) {
		double E = 30e3;
		double nu = 0.2;
		double t = 1;
		State ss = State.PLANE_STRESS;
		MaterialModel mm = new StVenantKirchhoffMM(E, nu, t, 0, ss);
		return mm;
	}

	@Override
	public Element createElement(MeshPart mp, MaterialModel mm) {
		double ft = 4;
		double Gf = 0.1;
		double H = -(ft * ft) / (2 * Gf);
		double km = 0.0;
		Element e = new ConstantCrackElement(mp, mm, H, km, ft);
		e.setEchoLevel(EchoLevelType.OUTPUT);
		return e;
	}

	public static void main(String[] args) throws IOException {

		AbstractStructuralStaticDemo sample = new FabienSampleConstantCrack();
		double[] lambda = MathUtilities.createLambdaArray(3.75, 0.1, 5);

		sample.run(lambda);

	}

}
